import { ipcMain, BrowserWindow } from 'electron';
import IpcParams, { gEventHamster } from './IpcParams';
import Config from '@/utils/config';
import { Api, TelegramClient } from 'telegram'
import { StringSession } from 'telegram/sessions'
import HamsterGame from './game/hamster'
import PiggyGame from './game/piggy'
import BlumGame from './game/blum'
import TonstationGame from './game/tonstation';
import DuckchainGame from './game/duckchain';
import TimefarmGame from './game/timefarm';
import TabizooGame from './game/tabizoo';
import CatsGame from './game/cats';
import MajorGame from './game/major';
import MemefiGame from './game/memefi';
import CowsGame from './game/cows';
import TomarketGame from './game/tomarket';
import SidekickGame from './game/sidekick';


/**
 * 单向调用
 * @param {*} event 
 * @param {IpcParams} ipcParams 
 */
function handleGameCall(event, ipcParams) {
  console.log(event, ipcParams)
  // const notifyParams = new IpcParams(gEventHamster, ["a", "b"])
  // console.log("asd", notifyParams)
  // setTimeout(() => {
  //   BrowserWindow.getFocusedWindow().webContents.send("game.notify", notifyParams)
  // }, 1000);
}

/**
 * 双向调用
 * @param {*} event 
 * @param {IpcParams} ipcParams
 */
async function handleGameInvoke(event, ipcParams) {
  // console.log(event, ipcParams)
  if (ipcParams.event == "requestTgwebUrl") {
    return await requestTgwebUrl(ipcParams)
  } else if (ipcParams.event == "kickoutOtherDevices") {
    return await kickoutOtherDevices(ipcParams)
  } else if (ipcParams.event == "runHamster") {
    const game = new HamsterGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runPiggy") {
    const game = new PiggyGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runBlum") {
    const game = new BlumGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runTonstation") {
    const game = new TonstationGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runDuckchain") {
    const game = new DuckchainGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runTimefarm") {
    const game = new TimefarmGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runTabizoo") {
    const game = new TabizooGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runCats") {
    const game = new CatsGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runMajor") {
    const game = new MajorGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runMemefi") {
    const game = new MemefiGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runCows") {
    const game = new CowsGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runTomarket") {
    const game = new TomarketGame(ipcParams.params)
    const result = await game.run()
    return result
  } else if (ipcParams.event == "runSidekick") {
    const game = new SidekickGame(ipcParams.params)
    const result = await game.run()
    return result
  } else {
    return null
  }
}


/**
 * 获取tgwbe-url
 * @param {IpcParams} ipcParams ipcParams.params: 账号相关的必要信息，bot、session、url、platform等
 * @returns 
 */
async function requestTgwebUrl(ipcParams) {
  const params = ipcParams.params
  const strSession = new StringSession(params.session)
  const client = new TelegramClient(strSession, Config.API_ID, Config.API_HASH, {
    connectionRetries: 3,
    useWSS: true,
    timeout: 20,
  })
  try {
    await client.connect();
    const isAuth = await client.checkAuthorization()
    if (isAuth != true) {
      return new IpcParams(ipcParams.event, ipcParams.params, "该账号未登录")
    }
    const user = await client.getInputEntity(params.bot);
    const result = await client.invoke(
      new Api.messages.RequestWebView({
        peer: user,
        bot: params.bot,
        fromBotMenu: true,
        url: params.url,
        platform: params.url,
      })
    );
    const url = result.url;
    return url
  } catch (error) {
    console.error(error)
    return new IpcParams(ipcParams.event, ipcParams.params, error)
  }
}

async function kickoutOtherDevices(params) {
  // todo
}

export function registerOnIpcMain() {
  ipcMain.on("game.call", handleGameCall)
  ipcMain.handle("game.invoke", handleGameInvoke)
}
